import datetime

import mongoengine as db

from app.db import BaseDocument, BaseEmbeddedDocument


class Params(BaseEmbeddedDocument):
    job_ids = db.ListField(db.DictField(default={}), default=[])
    download_type = db.StringField(default='')
    report_type = db.StringField(default='')
    start_date = db.DateTimeField(default=None)
    end_date = db.DateTimeField(default=None)


class Result(BaseEmbeddedDocument):
    file = db.FileField()
    filename = db.StringField(default='')
    status = db.StringField(default='')
    process = db.IntField(min_value=0, default=0)
    create_at = db.DateTimeField(default=None)


class CeleryTask(BaseDocument):
    """
    Args:
        db ([type]): [description]
    """
    celery_task_id = db.StringField(default='')
    task_type = db.StringField(required=True)
    params = db.EmbeddedDocumentField(Params, default=Params)
    result = db.EmbeddedDocumentField(Result, default=Result)
    operator = db.StringField(default='')
    create_time = db.DateTimeField(required=True)
    meta = {
        'collection': 'celery_task',
        'strict': False
    }

    def save(self, *args, **kwargs):
        if not self.create_time:
            self.create_time = datetime.datetime.utcnow()
        return super(CeleryTask, self).save(*args, **kwargs)
